package com.amos.teaching_aids.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.amos.teaching_aids.R;
import com.amos.teaching_aids.SysApplication;
import com.amos.teaching_aids.TitleLayout;
import com.amos.teaching_aids.presenter.IUserPresenter;
import com.amos.teaching_aids.presenter.UserPresenter;
import com.amos.teaching_aids.ui.Interface.IUserLoginView;
import com.amos.teaching_aids.ui.Interface.IUserRegisterView;

/**
 * Created by Administrator on 2016/10/9 0009.
 */

public class UserRegisterActivity extends BaseActivity implements IUserRegisterView{
    private EditText userId, userPass, userPass2, userName, userEmail, userMajor, userClass;
    private String identify;
    private TitleLayout titleLayout;
    private Button buttonConfirm;
    private IUserPresenter mUserPresenter;

    @Override
    public int getLayout() {
        return R.layout.activity_register;
    }

    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleLayout = (TitleLayout) findViewById(R.id.title);
        titleLayout.setTitle("注册");
        mUserPresenter = new UserPresenter(null, null, this);
        identify = mUserPresenter.getIdentify();
        if(identify.equals("teacher")) userClass.setVisibility(View.GONE);

    }

    @Override
    protected void initView() {
        super.initView();
        progressDialog.setMessage("注册中...");
        progressDialog.setCancelable(false);
        userId = (EditText) findViewById(R.id.reg_id);
        userPass = (EditText) findViewById(R.id.reg_pass);
        userPass2 = (EditText) findViewById(R.id.reg_pass2);
        userName = (EditText) findViewById(R.id.reg_name);
        userEmail = (EditText) findViewById(R.id.reg_email);
        userMajor = (EditText) findViewById(R.id.reg_major);
        userClass = (EditText) findViewById(R.id.reg_class);
        buttonConfirm = (Button) findViewById(R.id.button_confirm2);
        buttonConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });

    }

    private void register(){
        if(!userPass.getText().toString().equals(userPass2.getText().toString())){
            Toast.makeText(this, "两次密码输入不同，请重新输入", Toast.LENGTH_SHORT).show();
            clearUserPassword();
            clearUserPassword2();
        }
        else{
            mUserPresenter.register(userId.getText().toString(), userPass.getText().toString(),
                    userName.getText().toString(), userMajor.getText().toString(),
                    userEmail.getText().toString(), userClass.getText().toString(), identify);
        }
    }
    @Override
    public void clearUserId() {

    }

    @Override
    public void clearUserName() {

    }

    @Override
    public void clearUserPassword() {
        userPass.setText("");
    }

    @Override
    public void clearUserPassword2() {
        userPass2.setText("");
    }

    @Override
    public void clearUserEmail() {

    }

    @Override
    public void clearUserMajor() {

    }

    @Override
    public void clearUserClass() {

    }

    @Override
    public void showRegSuc() {
        Toast.makeText(SysApplication.getContext(), "注册成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showRegFail(String errorReason) {
        Toast.makeText(SysApplication.getContext(), "注册失败," + errorReason, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoding() {
        this.progressDialog.show();
    }

    @Override
    public void hideLoding() {
        this.progressDialog.dismiss();
    }


}
